const crypto = require('crypto');
const config = require('../config/sysConf.json');
const {
	accountColPromise,
} = require('./db-mongo')
const {
	getTime,
} = require('../utils/dateUtils')

/**
 * 初始化admin账户
 * @return {[type]} [description]
 */
module.exports = async function() {
	let accountCol = await accountColPromise
	let account = await accountCol.findOne({
		role: 0,
	})
	if (account && account._id)
		return
	return await accountCol.insertOne({
		mobile: config.adminAccount.mobile,
		password: encPwd(config.adminAccount.password),
		nickname: 'Admin',
		create_time: getTime(),
		modify_time: getTime(),
		role: 0,
	})
}

/**
 * 密码加密
 * @param  {[type]} password [description]
 * @return {[type]}          [description]
 */
function encPwd(password) {
	return crypto.createHash('md5').update(password).digest('hex');
}